package com.dap.cms.governance.controller;

import com.dap.cms.governance.po.CmsComponentInfoEnvPo;
import com.dap.cms.governance.service.CmsComponentInfoEnvService;
import com.dap.cms.governance.vo.QueryInfoVo;
import com.dap.dao.model.QueryResult;
import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: t_cms_component_info_env表对应的控制层类
 * @author: Fesine
 * @createTime:2018/09/11
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/09/11
 */
@RestController
@RequestMapping("/v1")
public class CmsComponentInfoEnvController {
    private static final Logger logger = LoggerFactory.getLogger(CmsComponentInfoEnvController.class);
    @Autowired
    private CmsComponentInfoEnvService cmsComponentInfoEnvService;

    /**
     * 新增CmsComponentInfoEnvPo
     *
     * @param cmsComponentInfoEnvPo
     * @return result
     */
    @PostMapping("/cmsComponentInfoEnv")
    public Result addCmsComponentInfoEnv(CmsComponentInfoEnvPo cmsComponentInfoEnvPo) {
        try {
            cmsComponentInfoEnvService.save(cmsComponentInfoEnvPo);
            return ResultUtils.success(ResultEnum.CREATED, null);
        } catch (Exception e) {
            logger.error("新增失败", e);
            return ResultUtils.error(500, "新增失败");
        }

    }

    /**
     * 更新CmsComponentInfoEnvPo
     *
     * @param cmsComponentInfoEnvPo
     * @return result
     */
    @PutMapping("/cmsComponentInfoEnv")
    public Result updateCmsComponentInfoEnv(CmsComponentInfoEnvPo cmsComponentInfoEnvPo) {
        try {
            cmsComponentInfoEnvService.update(cmsComponentInfoEnvPo);
            return ResultUtils.success(ResultEnum.CREATED, null);
        } catch (Exception e) {
            logger.error("更新失败", e);
            return ResultUtils.error(500, "更新失败");
        }

    }

    /**
     * 删除CmsComponentInfoEnvPo
     *
     * @param id
     * @return result
     */
    @DeleteMapping("/cmsComponentInfoEnv/{id}")
    public Result deleteCmsComponentInfoEnv(@PathVariable("id") Integer id) {
        CmsComponentInfoEnvPo cmsComponentInfoEnvPo = new CmsComponentInfoEnvPo();
        cmsComponentInfoEnvPo.setId(id);
        cmsComponentInfoEnvService.delete(cmsComponentInfoEnvPo);
        return ResultUtils.success(cmsComponentInfoEnvPo);
    }

    /**
     * 查看CmsComponentInfoEnvPo详情
     *
     * @param id
     * @return result
     */
    @GetMapping("/cmsComponentInfoEnv/{id}")
    public Result getCmsComponentInfoEnv(@PathVariable("id") Integer id) {
        CmsComponentInfoEnvPo cmsComponentInfoEnvPo = new CmsComponentInfoEnvPo();
        cmsComponentInfoEnvPo.setId(id);
        cmsComponentInfoEnvPo = cmsComponentInfoEnvService.get(cmsComponentInfoEnvPo);
        return ResultUtils.success(cmsComponentInfoEnvPo);
    }

    /**
     * 查询CmsComponentInfoEnvPo列表
     *
     * @param cmsComponentInfoEnvPo
     * @return result
     */
    @GetMapping("/cmsComponentInfoEnvs")
    public Result listAll(CmsComponentInfoEnvPo cmsComponentInfoEnvPo) {
        List<CmsComponentInfoEnvPo> list = cmsComponentInfoEnvService.listAll(cmsComponentInfoEnvPo);
        return ResultUtils.success(list);
    }

    /**
     * 分页查询QueryInfoVo列表
     *
     * @param queryInfoVo
     * @param pageIndex
     * @param pageSize
     * @return result
     */
    @GetMapping("/cmsComponentInfoEnvs/page")
    public Result listPage(QueryInfoVo queryInfoVo, @RequestParam(value = "pageIndex",
            required = false, defaultValue = "1")
            Integer pageIndex, @RequestParam(value = "pageSize", required = false, defaultValue =
            "10") Integer pageSize) {
        //根据组件名
        if (StringUtils.isNotEmpty(queryInfoVo.getComponentName())) {
            queryInfoVo.setComponentName("%".concat(queryInfoVo.getComponentName())
                    .concat("%"));
        } else {
            queryInfoVo.setComponentName(null);
        }
        //根据公司名
        if (StringUtils.isNotEmpty(queryInfoVo.getCompanyName())) {
            queryInfoVo.setCompanyName("%".concat(queryInfoVo.getCompanyName())
                    .concat("%"));
        } else {
            queryInfoVo.setCompanyName(null);
        }
        //根据系统名
        if (StringUtils.isNotEmpty(queryInfoVo.getSysName())) {
            queryInfoVo.setSysName("%".concat(queryInfoVo.getSysName())
                    .concat("%"));
        } else {
            queryInfoVo.setSysName(null);
        }
        //根据环境名
        if (StringUtils.isNotEmpty(queryInfoVo.getEnvName())) {
            queryInfoVo.setEnvName("%".concat(queryInfoVo.getEnvName())
                    .concat("%"));
        } else {
            queryInfoVo.setEnvName(null);
        }
        //根据环境名
        if (StringUtils.isNotEmpty(queryInfoVo.getVersionNo())) {
            queryInfoVo.setVersionNo("%".concat(queryInfoVo.getVersionNo())
                    .concat("%"));
        } else {
            queryInfoVo.setVersionNo(null);
        }
        QueryResult<QueryInfoVo> result = cmsComponentInfoEnvService.listQueryInfo(queryInfoVo, pageIndex, pageSize);
        long total = result.getTotalrecord();
        Map<String, Object> map = new HashMap(16);
        if (total > 0) {
            map.put("totalRecord", total);
            map.put("resultList", result.getResultlist());
        }
        return ResultUtils.success(map);
    }

}

